<template>
  <el-dialog
    :title="!dataForm.patientId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="患者id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="患者id"></el-input>
    </el-form-item>
    <el-form-item label="用户id（医保账号）" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="用户id（医保账号）"></el-input>
    </el-form-item>
    <el-form-item label="患者手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="患者手机号"></el-input>
    </el-form-item>
    <el-form-item label="患者年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="患者年龄"></el-input>
    </el-form-item>
    <el-form-item label="身高" prop="height">
      <el-input v-model="dataForm.height" placeholder="身高"></el-input>
    </el-form-item>
    <el-form-item label="体重" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="体重"></el-input>
    </el-form-item>
    <el-form-item label="肾功能" prop="renalFunction">
      <el-input v-model="dataForm.renalFunction" placeholder="肾功能"></el-input>
    </el-form-item>
    <el-form-item label="婚姻状况" prop="maritalStatus">
      <el-input v-model="dataForm.maritalStatus" placeholder="婚姻状况"></el-input>
    </el-form-item>
    <el-form-item label="生育状况" prop="fertilityStatus">
      <el-input v-model="dataForm.fertilityStatus" placeholder="生育状况"></el-input>
    </el-form-item>
    <el-form-item label="疾病史" prop="diseasesHistory">
      <el-input v-model="dataForm.diseasesHistory" placeholder="疾病史"></el-input>
    </el-form-item>
    <el-form-item label="肝功能" prop="liverFunction">
      <el-input v-model="dataForm.liverFunction" placeholder="肝功能"></el-input>
    </el-form-item>
    <el-form-item label="问诊次数" prop="treatNum">
      <el-input v-model="dataForm.treatNum" placeholder="问诊次数"></el-input>
    </el-form-item>
    <el-form-item label="支付总额" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="支付总额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          patientId: 0,
          userId: '',
          patientName: '',
          phone: '',
          age: '',
          height: '',
          weight: '',
          renalFunction: '',
          maritalStatus: '',
          fertilityStatus: '',
          diseasesHistory: '',
          liverFunction: '',
          treatNum: '',
          totalPrice: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '患者id不能为空', trigger: 'blur' }
          ],
          patientName: [
            { required: true, message: '用户id（医保账号）不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '患者手机号不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '患者年龄不能为空', trigger: 'blur' }
          ],
          height: [
            { required: true, message: '身高不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '体重不能为空', trigger: 'blur' }
          ],
          renalFunction: [
            { required: true, message: '肾功能不能为空', trigger: 'blur' }
          ],
          maritalStatus: [
            { required: true, message: '婚姻状况不能为空', trigger: 'blur' }
          ],
          fertilityStatus: [
            { required: true, message: '生育状况不能为空', trigger: 'blur' }
          ],
          diseasesHistory: [
            { required: true, message: '疾病史不能为空', trigger: 'blur' }
          ],
          liverFunction: [
            { required: true, message: '肝功能不能为空', trigger: 'blur' }
          ],
          treatNum: [
            { required: true, message: '问诊次数不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '支付总额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.patientId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.patientId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patientpatient/info/${this.dataForm.patientId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.patientPatient.userId
                this.dataForm.patientName = data.patientPatient.patientName
                this.dataForm.phone = data.patientPatient.phone
                this.dataForm.age = data.patientPatient.age
                this.dataForm.height = data.patientPatient.height
                this.dataForm.weight = data.patientPatient.weight
                this.dataForm.renalFunction = data.patientPatient.renalFunction
                this.dataForm.maritalStatus = data.patientPatient.maritalStatus
                this.dataForm.fertilityStatus = data.patientPatient.fertilityStatus
                this.dataForm.diseasesHistory = data.patientPatient.diseasesHistory
                this.dataForm.liverFunction = data.patientPatient.liverFunction
                this.dataForm.treatNum = data.patientPatient.treatNum
                this.dataForm.totalPrice = data.patientPatient.totalPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patientpatient/${!this.dataForm.patientId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'patientId': this.dataForm.patientId || undefined,
                'userId': this.dataForm.userId,
                'patientName': this.dataForm.patientName,
                'phone': this.dataForm.phone,
                'age': this.dataForm.age,
                'height': this.dataForm.height,
                'weight': this.dataForm.weight,
                'renalFunction': this.dataForm.renalFunction,
                'maritalStatus': this.dataForm.maritalStatus,
                'fertilityStatus': this.dataForm.fertilityStatus,
                'diseasesHistory': this.dataForm.diseasesHistory,
                'liverFunction': this.dataForm.liverFunction,
                'treatNum': this.dataForm.treatNum,
                'totalPrice': this.dataForm.totalPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
